﻿Imports System.Data
Imports System.Data.SqlClient

Partial Class configuration
    Inherits System.Web.UI.Page

#Region "champs"
    Dim sysConfig As dataConfiguration
    Private listSitesWeb As New Collection()
    Private listServeurs As New Collection()
    Private listWebServices As New Collection()

    'Variables du processus KelControl
    Dim sLocationKelControler As String = "C:\Documents and Settings\KelStage\Bureau\KelControl\bin\Release\KelControl.exe"
    Dim isKelContoler As Boolean
    Dim pKelContoler As System.Diagnostics.Process

    'Variables des données (SGBD)
    Private sConnexion As String = "Data Source=POSTE-DEV16;Initial Catalog=KelControl;User ID=sa;Password=kelstage"
    Private sqlConnexion As SqlConnection
    Private scConnexion As SqlCommand
    Private dataConnexion As New SqlDataAdapter
    Private dataReadConnexion As SqlDataReader
    Private dsConnexion As New DataSet
#End Region

#Region "Methodes"
    Private Sub recupererConfiguration()
        Dim sRequete = "SELECT sites_timer, sites_erreurReport, serveurs_timer, serveurs_erreurReport, " & _
                       "services_timer, services_erreurReport FROM Config_system"
        Dim row As Object() = Nothing

        scConnexion = New SqlCommand(sRequete, sqlConnexion)
        dataReadConnexion = scConnexion.ExecuteReader

        While dataReadConnexion.Read
            If row Is Nothing Then
                row = New Object(dataReadConnexion.FieldCount) {}
            End If
            dataReadConnexion.GetValues(row)
            sysConfig.iTimerSites = row(0)
            sysConfig.iNbErreurSites = row(1)
            sysConfig.iTimerServeurs = row(2)
            sysConfig.iNbErreurServeurs = row(3)
            sysConfig.iTimerServices = row(4)
            sysConfig.iNbErreurServices = row(5)
        End While
        dataReadConnexion.Dispose()
    End Sub

    Private Sub recupererListeSites()
        Dim sRequete = "SELECT url FROM Config_sites"
        Dim row As Object() = Nothing

        scConnexion = New SqlCommand(sRequete, sqlConnexion)
        dataReadConnexion = scConnexion.ExecuteReader

        While dataReadConnexion.Read
            If row Is Nothing Then
                row = New Object(dataReadConnexion.FieldCount - 1) {}
            End If
            dataReadConnexion.GetValues(row)
            listSitesWeb.Add(Trim(row(0).ToString))
        End While
        dataReadConnexion.Dispose()
    End Sub

    Private Sub recupererListeServeurs()
        Dim sRequete = "SELECT ip FROM Config_serveurs"
        Dim row As Object() = Nothing

        scConnexion = New SqlCommand(sRequete, sqlConnexion)
        dataReadConnexion = scConnexion.ExecuteReader

        While dataReadConnexion.Read
            If row Is Nothing Then
                row = New Object(dataReadConnexion.FieldCount - 1) {}
            End If
            dataReadConnexion.GetValues(row)
            listServeurs.Add(Trim(row(0).ToString))
        End While
        dataReadConnexion.Dispose()
    End Sub

    Private Sub recupererListeServices()
        Dim sRequete = "SELECT url, methode FROM Config_services"
        Dim row As Object() = Nothing

        scConnexion = New SqlCommand(sRequete, sqlConnexion)
        dataReadConnexion = scConnexion.ExecuteReader

        While dataReadConnexion.Read
            If row Is Nothing Then
                row = New Object(dataReadConnexion.FieldCount - 1) {}
            End If
            dataReadConnexion.GetValues(row)
            Dim data(2) As String
            data(0) = Trim(row(0).ToString)
            data(1) = Trim(row(1).ToString)
            listWebServices.Add(data)
        End While
        dataReadConnexion.Dispose()
    End Sub

    Private Sub afficherConfiguration()
        iTimerSites.InnerHtml = sysConfig.iTimerSites.ToString & " min"
        iTimerServeurs.InnerHtml = sysConfig.iTimerServeurs.ToString & " min"
        iTimerServices.InnerHtml = sysConfig.iTimerServices.ToString & " min"
        iNbErreurSites.InnerHtml = sysConfig.iNbErreurSites.ToString
        iNbErreurServeurs.InnerHtml = sysConfig.iNbErreurServeurs.ToString
        iNbErreurServices.InnerHtml = sysConfig.iNbErreurServices.ToString
    End Sub

    Private Sub afficherListeSites()
        Dim addSiteHTML = liSite.InnerHtml
        liSite.InnerHtml = ""
        Dim listContent As String
        For i = 1 To listSitesWeb.Count Step 1
            listContent = "<li>"
            listContent &= "<div class='nom'>" & listSitesWeb(i).ToString & "</div>"
            listContent &= "<div class='btnPlus'>[Supprimer]</div>"
            listContent &= "<div class='clear'></div>"
            liSite.InnerHtml &= listContent
        Next
        If (listSitesWeb.Count = 0) Then
            listContent = "<li>"
            listContent &= "<div class='nom'>Aucun serveur configuré</div>"
            listContent &= "<div class='clear'></div>"
            liSite.InnerHtml &= listContent
        End If
        liSite.InnerHtml &= addSiteHTML
    End Sub

    Private Sub afficherListeServeurs()
        Dim addServeursHTML = liServeurs.InnerHtml
        liServeurs.InnerHtml = ""
        Dim listContent As String
        For i = 1 To listServeurs.Count Step 1
            listContent = "<li>"
            listContent &= "<div class='nom'>" & listServeurs(i).ToString & "</div>"
            listContent &= "<div class='btnPlus'>[Supprimer]</div>"
            listContent &= "<div class='clear'></div>"
            liServeurs.InnerHtml &= listContent
        Next
        If (listServeurs.Count = 0) Then
            listContent = "<li>"
            listContent &= "<div class='nom'>Aucun serveur configuré</div>"
            listContent &= "<div class='clear'></div>"
            liServeurs.InnerHtml &= listContent
        End If
        liServeurs.InnerHtml &= addServeursHTML
    End Sub

    Private Sub afficherListeServices()
        Dim addServicesHTML = liServices.InnerHtml
        liServices.InnerHtml = ""
        Dim listContent As String
        For i = 1 To listWebServices.Count Step 1
            Dim data(2) As String
            data = listWebServices(i)
            listContent = "<li>"
            listContent &= "<div class='nom'>" & data(0) & "/" & data(1) & "</div>"
            listContent &= "<div class='btnPlus'>[Supprimer]</div>"
            listContent &= "<div class='clear'></div>"
            liServices.InnerHtml &= listContent
        Next
        If (listWebServices.Count = 0) Then
            listContent = "<li>"
            listContent &= "<div class='nom'>Aucun service configuré</div>"
            listContent &= "<div class='clear'></div>"
            liServices.InnerHtml &= listContent
        End If
        liServices.InnerHtml &= addServicesHTML
    End Sub

    Public Sub startControler()
        If (isKelContoler) Then
            pKelContoler.Kill()
            isKelContoler = False
            System.Threading.Thread.Sleep(3000)
            startControler()
        Else
            pKelContoler = System.Diagnostics.Process.Start(sLocationKelControler)
            restartSystem.CssClass = "etatOk"
            restartSystem.Text = "Application lancée, cliquez pour re-démarrer"
        End If
    End Sub
#End Region

#Region "Events"
    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        sqlConnexion = New SqlConnection()
        sqlConnexion.ConnectionString = sConnexion
        sqlConnexion.Open()
        recupererConfiguration()
        recupererListeSites()
        recupererListeServeurs()
        recupererListeServices()
        sqlConnexion.Dispose()
        sqlConnexion.Close()

        afficherListeSites()
        afficherListeServeurs()
        afficherListeServices()
        afficherConfiguration()

        isKelContoler = System.Diagnostics.Process.GetProcessesByName("KelControl").Length > 0

        If (isKelContoler) Then
            pKelContoler = System.Diagnostics.Process.GetProcessesByName("KelControl")(0)
        End If

        If Not (IsPostBack) Then
            If (isKelContoler) Then
                restartSystem.CssClass = "etatOk"
                restartSystem.Text = "Application lancée, cliquez pour re-démarrer"
            Else
                restartSystem.CssClass = "etatErreur"
                restartSystem.Text = "Application arrété, cliquez pour la lancer"
            End If
        End If

    End Sub

    Protected Sub restartSystem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles restartSystem.Click
        startControler()
    End Sub
#End Region

End Class
